package leetcode;

public class _2038 {
    /**
     * 如果相邻两个颜色均相同则删除当前颜色
     */
    class Solution {
        public boolean winnerOfGame(String colors) {
            char[] cs = colors.toCharArray();
            int n = cs.length;
            int a = 0, b = 0;
            for (int i = 1; i < n - 1; i++) {
                if (cs[i] == 'A' && cs[i - 1] == 'A' && cs[i + 1] == 'A') a++;
                if (cs[i] == 'B' && cs[i - 1] == 'B' && cs[i + 1] == 'B') b++;
            }
            return a > b;
        }
    }

    class Solution2 {
        public boolean winnerOfGame(String colors) {
            char[] cs = colors.toCharArray();
            int n = cs.length;
            int a = 0, b = 0;
            int i=0;
            while(i<n){
                if(cs[i]=='A'){
                    int start = i;
                    i++;
                    while(i<n&&cs[i]=='A'){
                        i++;
                    }
                    if(i-start>=3){
                        a = a + i-start-2;
                    }
                }
                if(i<n&&cs[i]=='B'){
                    int start = i;
                    i++;
                    while(i<n&&cs[i]=='B'){
                        i++;
                    }
                    if(i-start>=3){
                        b = b+i-start-2;
                    }
                }
            }
            return a > b;
        }
    }
}
